package cn.wolfcode.Controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.domain.Role;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.service.PermissionService;
import cn.wolfcode.service.RoleService;
import cn.wolfcode.service.impl.RoleServiceImpl;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServiceImpl roleService;

    @Autowired
   private PermissionService permissionService;






//    @RequestMapping("/list")
//    public String listAll(Model model){
//        List<Role> roles = roleMapper.selectAll();
//        model.addAttribute("roles",roles);
//        return "role/list";
//    }

    @RequirePermission(name = "查看角色",experssion = "role:list")
    @RequestMapping("/list")
    public String listAll(Model model,QueryObject QueryObject){
        PageInfo<Role> pageInfo = roleService.listPage(QueryObject);
        model.addAttribute("pageInfo",pageInfo);
        return "role/list";
    }

    @RequirePermission(name = "删除角色",experssion = "role:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            roleService.deleteByPrimaryKey(id);
        }
        return "redirect:/role/list";
    }

    @RequirePermission(name = "角色新增/编辑",experssion = "role:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
//        List<Department> departments = departmentService.selectAll();
//        model.addAttribute("departments", departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);
        if (id != null){
            Role role = roleService.selectByPrimaryKey(id);
            model.addAttribute("role", role);
        }
        return "role/input";
    }

    @RequirePermission(name = "角色新增/编辑",experssion = "role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Model model, Long[] ids){
        System.out.println(role.getId());
        if (role.getId() != null){
            roleService.updateByPrimaryKey(role);
            //先删除之前的关联关系在进行重新关联插入
            //这样不需要判断
            //删除关系
            roleService.deletePRelation(role.getId());
            //添加关系
//            roleService.insertRelation(role.getId(), ids);
        }else {
            roleService.insert(role);
            //由于是是添加,所以不需要删除关系,进行添加关系即可
//            roleService.insertRelation(role.getId(), ids);
        }
        if (ids != null && ids.length>0){
            roleService.insertPRelation(role.getId(), ids);
        }

        return "redirect:/role/list";
    }


}

